
import IndexService from '@/service/index';

const indexService = new IndexService();
const user = {
  state: {
    name: '',
    userId: ''
  },

  mutations: {
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_USER_ID: (state, userId) => {
      state.userId = userId
    }
  },

  actions: {

    // 登陆
    Login({ commit,state },userInfo) {
      return new Promise((resolve, reject) => {
        indexService.login(userInfo.userName,userInfo.password).then(res => {
          commit('SET_NAME', res.name)
          commit('SET_USER_ID', res.userId)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout({ commit,state }) {
      return new Promise((resolve, reject) => {
        indexService.logout().then((res) => {
          commit('SET_NAME', '')
          commit('SET_USER_ID', '')
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
  }
}

export default user
